package cn.onesdream.app.api.service.Impl;

import cn.onesdream.app.api.service.FriendService;
import cn.onesdream.app.dao.entity.Card;
import cn.onesdream.app.dao.entity.Friend;
import cn.onesdream.app.dao.mapper.FriendMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author WangShiyu
 * @date 2021/3/13 12:23
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {

    @Override
    public void deleteFriend(Integer userId, Integer friendUserId) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("friend_user_id", friendUserId);
        baseMapper.delete(wrapper);
    }

    @Override
    public void addFriend(Integer userId, Integer friendUserId) {
        Friend friend = baseMapper.getLostFriendById(userId, friendUserId);
        if(friend == null){
            friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendUserId(friendUserId);
            baseMapper.insert(friend);
        }
        else {
            friend.setActive((byte) 1);
            baseMapper.addLostFriend(friend);
        }
    }

    @Override
    public List<Friend> getFriend(Integer userId) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Friend> friendList = baseMapper.selectList(wrapper);
        return friendList;
    }
}
